const express = require('express');
const router = express.Router();
const { login } = require('../models/user');

router.post('/login', (req, res) => {
    console.log(req.body);
    let { username, password } = req.body;
    let email = username;
    login(email, password, (err, user, mes) => {
        if (err) {
            return res.status(500).json({
                error: 'Internal Server Error'
            });
        }
        if (!user) {
            return res.status(201).json({
                error: mes
            });
        } else {
            res.status(200).json({
                token: 'Login successful'
            });
        }
    })

});

router.get('/logout', (req, res) => {
    res.send('Hello World!');
});

module.exports = router;